<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Xnb extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->verify_user();
	}

	public function index()
	{
		$d['coins'] = $this->d->rows(array('table' => 'coin'));
		$this->view('xnb', $d);
	}

	public function data()
	{
		$page = ($this->input->post('page') ? intval($this->input->post('page')) : 1);
		$rows = ($this->input->post('rows') ? intval($this->input->post('rows')) : 10);
		$id = intval($this->input->post('id'));
		$username = $this->input->post('username');
		$coin_id = $this->input->post('coin_id');
		$where[1] = 1;

		if ($username) {
			$user = $this->db->where('username', $username)->get('user')->row_array();
			$where['user_id'] = $user['id'];
		}

		if ($id) {
			$where['id'] = $id;
		}

		if ($coin_id) {
			$where['coin_id'] = $coin_id;
		}

		$result['total'] = $this->db->where($where)->count_all_results('xnb');
		$start = ($page - 1) * $rows;
		$result['rows'] = $this->db->where($where)->limit($rows, $start)->get('xnb')->result_array();

		foreach ($result['rows'] as $k => $v) {
			$result['rows'][$k]['total'] = $v['num'] + $v['num_frozen'];
			$result['rows'][$k]['username'] = $this->d->find('username', 'user', 'id=' . $v['user_id']);
			$result['rows'][$k]['coin_name'] = $this->d->find('cn_name', 'coin', 'id=' . $v['coin_id']);
		}

		echo json_encode($result);
	}

	public function info($id)
	{
		$id = intval($id);
		$d = $this->db->where('id', $id)->get('user')->row_array();
		$this->load->view('user_info', $d);
	}

	public function fill($id)
	{
		$id = intval($id);
		$d = $this->db->where('id', $id)->get('xnb')->row_array();
		$d['username'] = $this->d->field(array('field' => 'username', 'table' => 'user', 'where' => 'id=' . $d['user_id']));
		$this->load->view('xnb_fill', $d);
	}

	public function update()
	{
		$id = intval($this->input->post('id'));
		$num = num($this->input->post('num'));

		if (!$id) {
			ajax_return(1, '找不到账户');
		}

		$this->db->query('set autocommit=0');
		$this->db->query('lock tables t_xnb write');
		$rs = $this->db->where('id', $id)->set('num', $num)->update('xnb');

		if ($rs) {
			$this->db->query('commit');
			$this->db->query('unlock tables');
			ajax_return(0, '修改成功');
		}
		else {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '修改失败');
		}
	}
}

?>
